
import 'package:flame/collisions.dart';
import 'package:flame/game.dart';
import 'package:flame/events.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:study_demo/ui/game/touch_indicator.dart';

import '../component.dart';

class Game4 extends FlameGame with KeyboardEvents, TapDetector, PanDetector {
  late final HeroComponent player;
  late final Monster monster;
  late final RectangleHitbox box;

  @override
  Future<void> onLoad() async {
    player = HeroComponent();
    add(player);
  }

  final double step = 10;

  double _counter = 0;

  @override
  void onTap() {}

  @override
  onTapCancel() {}

  @override
  onTapDown(TapDownInfo info) {
    Vector2 target = info.eventPosition.global;
    add(TouchIndicator(position: target));
    player.toTarget(target);
  }

  @override
  onTapUp(TapUpInfo info) {}

  @override
  void onPanStart(DragStartInfo info) {}

  double ds = 0;

  @override
  void onPanUpdate(DragUpdateInfo info) {
    ds += info.delta.global.length;
    if (ds > 10) {
      add(TouchIndicator(position: info.eventPosition.global));
      ds = 0;
    }
  }

  @override
  void onPanEnd(DragEndInfo info) {}

  @override
  void onPanCancel() {}

  // 键盘事件
  @override
  KeyEventResult onKeyEvent(
      KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
    return super.onKeyEvent(event, keysPressed);
  }
}
